package fun.yao.common.monitor.info;

import fun.yao.common.Constant;
import fun.yao.common.msg.HashtableMsg;
import fun.yao.common.uitls.LogUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * 数据库连接池信息
 * @author longjinglai
 *
 */
public class DbConnPoolInfo implements IMonitorInfo {

    private List<DbConnPoolDetail> alDbConnPoolDetail = new ArrayList<DbConnPoolDetail>();

    public List<DbConnPoolDetail> getDbConnPoolDetail()
    {
        return this.alDbConnPoolDetail;
    }

    public DbConnPoolInfo()
    {


    }

    public DbConnPoolInfo(HashtableMsg htm)
    {
        for(int i=0;i<htm.size();i++)
        {
            Hashtable<String, String> mapDbConnPoolInfo = htm.get(i);
            if(mapDbConnPoolInfo!=null)
            {
                DbConnPoolDetail detail = new DbConnPoolDetail(mapDbConnPoolInfo.get("pool_name"),Integer.parseInt(mapDbConnPoolInfo.get("pool_size")) , Integer.parseInt(mapDbConnPoolInfo.get("use_count")),Integer.parseInt(mapDbConnPoolInfo.get("free_count")));
                this.alDbConnPoolDetail.add(detail);
            }
        }
    }

    /**
     * 转换为HashtableMsg
     * @return
     */
    public HashtableMsg toHashtableMsg()
    {
        HashtableMsg htm = new HashtableMsg();
        for(int i=0;i<this.alDbConnPoolDetail.size();i++)
        {
            Hashtable<String, String> mapDbConnPoolInfo = new Hashtable<String, String>();
            mapDbConnPoolInfo.put("pool_name", this.alDbConnPoolDetail.get(i).getPoolName());
            mapDbConnPoolInfo.put("pool_size", this.alDbConnPoolDetail.get(i).getPoolSize()+"");
            mapDbConnPoolInfo.put("use_count", this.alDbConnPoolDetail.get(i).getUseCount()+"");
            mapDbConnPoolInfo.put("free_count", this.alDbConnPoolDetail.get(i).getFreeCount()+"");
            htm.add(mapDbConnPoolInfo);
        }
        return htm;
    }

    @Override
    public String toString() {
        return "DbConnPoolInfo [alDbConnPoolDetail=" + alDbConnPoolDetail + "]";
    }

    public class DbConnPoolDetail
    {
        /**
         * 连接池名称
         */
        private String poolName = "";

        /**
         * 连接池大小
         */
        private int poolSize = 0;

        /**
         * 使用数量
         */
        private int useCount = 0;

        /**
         * 空闲数量
         */
        private int freeCount = 0;

        public DbConnPoolDetail(String poolName, int poolSize, int useCount, int freeCount)
        {
            this.poolName = poolName;
            this.poolSize = poolSize;
            this.useCount = useCount;
            this.freeCount = freeCount;
        }

        public String getPoolName() {
            return poolName;
        }

        public void setPoolName(String poolName) {
            this.poolName = poolName;
        }

        public int getPoolSize() {
            return poolSize;
        }

        public void setPoolSize(int poolSize) {
            this.poolSize = poolSize;
        }

        public int getUseCount() {
            return useCount;
        }

        public void setUseCount(int useCount) {
            this.useCount = useCount;
        }

        public int getFreeCount() {
            return freeCount;
        }

        public void setFreeCount(int freeCount) {
            this.freeCount = freeCount;
        }

        @Override
        public String toString() {
            return "DbConnPoolDetail [poolName=" + poolName + ", poolSize="
                    + poolSize + ", useCount=" + useCount + ", freeCount="
                    + freeCount + "]";
        }
    }

    public void print(StringBuilder sbOut) {
        try
        {
            for(int i=0;i<this.getDbConnPoolDetail().size();i++)
            {
                DbConnPoolDetail detailInfo = this.getDbConnPoolDetail().get(i);
                sbOut.append("第" + i + "个pool["+detailInfo.getPoolName()+"]使用情况\n");
                sbOut.append("pool名称："+detailInfo.getPoolName()+"，pool大小："+detailInfo.getPoolSize()+"，使用数量："+detailInfo.getUseCount()+"，剩余数量："+detailInfo.getFreeCount()+"\n");
            }
        }catch (Exception e) {
            LogUtil.WriteLog(Constant.BADLY_ERROR, e);
        }
    }
}

